package com.tao.design.observer

/**
 *
 * @ProjectName:    lab
 * @Package:        com.tao.design.observer
 * @ClassName:      observe
 * @Description:     java类作用描述
 * @Author:         wangxf
 * @CreateDate:     2022/4/24 14:29
 * @UpdateUser:     更新者
 * @UpdateDate:     2022/4/24 14:29
 * @UpdateRemark:   更新说明
 * @Version:        1.0
 */

/**
 * 气象站 观察者模式
 */
interface Subject {
    fun registerObserver(observer: Observer)
    fun removeObserver(observer: Observer)
    fun notifyObserver();
}

interface Observer {
    fun update();
}

class XxxSubject : Subject {
    var observerList: MutableList<Observer> = ArrayList();
    var change: Boolean = false;
    override fun registerObserver(observer: Observer) {
        observerList.add(observer)
    }

    override fun removeObserver(observer: Observer) {
        if (observerList.contains(observer)) {
            observerList.remove(observer);
        }
    }

    override fun notifyObserver() {
        if (change) {
            observerList.forEach {
                it.update()
            }
        }
    }
}